home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1443_working_with_the_Storage_class.rtf < prev    next >
Text File  |  1993-11-08  |  2KB  |  60 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f2\fswiss Helvetica;\f1\fmodern Courier;}
  2. \paperw12280
  3. \paperh8980
  4. \margl120
  5. \margr120
  6. {\colortbl;\red0\green0\blue0;\red84\green84\blue84;\red83\green83\blue83;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\b0\i0\ulnone\fs28\fc1\cf1 Q:  How do you store and access elements in the Storage class?\
  8. \
  9. A:  Although Storage's instance variables permit you to access the data directly, this approach is discouraged.   Instead, use the Storage object's methods (such as addElement: and elementAt:) to store and access the elements.  \
  10. \
  11. Following is a code snippet that creates an empty Storage object, adds new elements to it, and then references them.  The type-specific code is #defined for clarity.  Note that the elements need to be added and referenced as pointers.\
  12.  
  13. \pard\tx533\tx1067\tx1601\tx2135\tx2668\tx3202\tx3736\tx4270\tx4803\tx5337\f2\fs24\fc0\cf0 \
  14.  
  15. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f1\fc1\cf1     #import <objc/Storage.h>\
  16. \
  17.     @interface SomeObject:Object\
  18.     @end\
  19.     @implementation SomeObject\
  20. \
  21.     #if 1\
  22.     #define TYPE char *\
  23.     #define PRINT(var1, var2)  printf("first = %s second = %s\\n", var1, var2);\
  24.     #define VAL1 ("hello")\
  25.     #define VAL2 ("world")\
  26.     #else\
  27.     #define TYPE int\
  28.     #define PRINT(var1, var2)  printf("first = %d second = %d\\n", var1, var2);\
  29.     #define VAL1 (-5)\
  30.     #define VAL2 (32)\
  31.     #endif\
  32. \
  33.     - appDidInit:sender\
  34.     \{\
  35.         Storage *store;\
  36.         TYPE a = VAL1;\
  37.         TYPE b = VAL2;\
  38.         TYPE *a1;\
  39.         TYPE *b1;\
  40.     \
  41.         store = [[Storage alloc] initCount:0 elementSize:sizeof(TYPE)\
  42.                     description:@encode(TYPE)];\
  43.         [store addElement:(void *)&a];\
  44.         [store addElement:(void *)&b];\
  45.         PRINT(a, b);\
  46. \
  47.         a1 = (TYPE *) [store elementAt:0];\
  48.         b1 = (TYPE *) [store elementAt:1];\
  49.         PRINT(*a1, *b1);\
  50.     \
  51.         return self;\
  52.     \}\
  53.  
  54. \f0\fs28 \
  55. QA504    \
  56. \
  57. Valid for 2.0, 3.0\
  58. \
  59.  
  60.